﻿var setting = {
    data: {
        key: {
            title: "t"
        },
        simpleData: {
            enable: true
        }
    },
    callback: {
        onClick: onClick
    }
};
var zNodes = [];
$.ajax({
    type: 'post',
    url: '/Role/GetAllMenuList',
    dataType: 'json',
    success: function (data) {
        for (var i = 0, l = data.length; i < l; i++) {
            if (data[i].menuUrl === "" || data[i].menuUrl === null) {
                zNodes.push({ id: data[i].menuId, pId: data[i].menuParentId, name: data[i].menuName, open: true })
            }
            else {
                zNodes.push({ id: data[i].menuId, pId: data[i].menuParentId, name: data[i].menuName })
            }
        }
        $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    }
});
function onClick(event, treeId, treeNode, clickFlag) {
    var RoleId = getUrlParam("RoleId");
    if (RoleId === "" || RoleId === null)
    {
        RoleId = 1;
    }
    $.ajax({
        url: "/Button/GetButtonByMenuIdList",
        type: 'post',
        data: { MenuId: treeNode.id, RoleId: RoleId },
        dataType: "json",
        async: "false",
        success: function (data) {
            $("#MenuId").val();
            $("#MenuName").val();
            $("#MenuId").val(treeNode.id);
            $("#MenuName").val(treeNode.name);
            $("#trid").children().remove();
            for (var y = 0; y < data.length; y++) {
                $("#trid").append(
                    "<tr>" +
                    " <td ><input type='radio' name='id' value=" + data[y].buttonId + " title=''  > </td>"+
                    "<td>" + data[y].buttonId + "</td>" +
                    "<td>" + data[y].buttonName + "</td>" +
                    "<td>" + data[y].buttonKey + "</td>" +
                    "<td>" + data[y].themeName + "</td>" +
                    '<td><button id="' + data[y].buttonKey + '" class="' + data[y].styleCss + '"><i class="layui-icon" > ' + data[y].iocn + '</i> ' + data[y].buttonName + '</button></td>' +
                    "<td>" + data[y].orderBy + "</td>" +
                    '<td><div class="check-box"><div class="wrapper"><div class="box"><input type="checkbox" name="roleName" id="' + data[y].buttonId + '" ' + data[y].checked + '  /><span>√</span></div></div></div></td>' +
                    "</tr>");
            }
        }
    });
}

//保存设置  测试使用
function fun() {
    obj = document.getElementsByName("roleName");
    check_val = [];
    for (k in obj) {
        if (obj[k].checked)
            check_val.push(obj[k].id);
    }
    alert(check_val);
}
